﻿using System;
using System.Windows.Forms;

namespace Chromium_Updater
{
    public partial class CUUForm : Form
    {
        //
        private string _latest_url_switch;
        private string _specific_url_switch;

        public CUUForm(string latest_url_switch, string specific_url_switch)
        {
            InitializeComponent();
            //
            _latest_url_switch   = latest_url_switch;
            _specific_url_switch = specific_url_switch;
            //
            label2.Text = shortenURL(Settings.Latest_Revision_URL, 70);
            label7.Text = shortenURL(Settings.Specific_Revision_URL, 70);
            label3.Text = shortenURL(latest_url_switch, 70);
            label6.Text = shortenURL(specific_url_switch, 70);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.Latest_Revision_URL   = _latest_url_switch;
            Settings.Specific_Revision_URL = _specific_url_switch;
            //
            this.Close();
        }

        private string shortenURL(string url, int max)
        {
            if (url.Length <= max) { return url; }

            // Remove the protocol
            int i = url.IndexOf("://");
            if (i > -1)
            {
                url = url.Substring(i + 3);
                if (url.Length <= max) { return url; }
            }

            // Remove the folder structure
            i = url.IndexOf("/") + 1;
            int n = url.LastIndexOf("/");
            if (n > i)
            {
                url = url.Replace(url.Substring(i, n - i), "...");
                if (url.Length <= max) { return url; }
            }

            return url;
        }
    }
}
